package com.mobilesales.android.adapters;

import java.util.ArrayList;
import java.util.List;
import com.mobilesales.android.MobileSalesActivity;
import com.mobilesales.android.R;
import com.mobilesales.android.about.AboutActivity;
import com.mobilesales.android.activities.clients.Clients;
import com.mobilesales.android.activities.common.SettingsActivity;
import com.mobilesales.android.activities.orders.Orders;
import com.mobilesales.android.activities.products.Products;
import com.mobilesales.android.activities.tasks.Tasks;
import com.mobilesales.android.model.employees.Message;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SlidingMenuAdapter extends ArrayAdapter<String> {

	Context context;
	int ResId;
	List<String> lst;
	int isPos = -1;
	List<Class<?>> lstclss = new ArrayList<Class<?>>();

	public SlidingMenuAdapter(Context context, int resource,
			List<String> objects, Class<?> parentclass) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.ResId = resource;
		this.lst = objects;

		lstclss.add(MobileSalesActivity.class);
		lstclss.add(Products.class);
		lstclss.add(Clients.class);
		lstclss.add(Tasks.class);
		lstclss.add(Orders.class);
		lstclss.add(Message.class);
		lstclss.add(SettingsActivity.class);
		lstclss.add(AboutActivity.class);

		this.isPos = lstclss.indexOf(parentclass);
		if (isPos == 0) {
			for (int i = 1; i < lst.size() - 2;) {
				lst.remove(1);
				lstclss.remove(1);
			}
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(ResId, null);
		}

		final String temp = lst.get(position);

		if (temp != null) {
			View top = (View) v.findViewById(R.id.slidingmenu_line_top);
			View bottom = (View) v.findViewById(R.id.slidingmenu_line_bottom);
			TextView text = (TextView) v
					.findViewById(R.id.slidingmenu_list_item_text);
			text.setText(temp);
			if (position == lst.size() - 1) {
				top.setVisibility(View.VISIBLE);
			} else {
				if (position == 0)
					bottom.setVisibility(View.VISIBLE);
				else {
					top.setVisibility(View.GONE);
					bottom.setVisibility(View.GONE);
				}
			}

			if (position == isPos) {
				v.setBackgroundResource(R.drawable.border_blue_dark);
			} else {
				v.setBackgroundResource(android.R.drawable.list_selector_background);
			}

			v.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					onCLicks(position);
				}
			});
		}
		return v;
	}

	private final void onCLicks(int pos) {
		if (pos != isPos) {
			Intent intent = new Intent(context, lstclss.get(pos));
			context.startActivity(intent);
		}
	}
}
